package com.sun.module_common.widget

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import com.blankj.utilcode.util.TimeUtils
import com.sun.module_base.ext.click
import com.sun.module_base.widget.Material3Card
import com.sun.module_common.model.Article
import com.sun.module_common_res.R

/**
 *@author:sunc
 *@time :2025/8/1 16:15
 *@desc: 描述
 */

@Composable
fun ArticleItem(
    modifier: Modifier = Modifier,
    data: Article,
    index: Int,
    isTop: Boolean = false,
    itemClick: () -> Unit = {}
) {

    Material3Card(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(10.dp)
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            ConstraintLayout(
                modifier = modifier
                    .fillMaxSize()
                    .click { itemClick.invoke() }
                    .padding(vertical = 5.dp, horizontal = 10.dp)
            ) {
                val (head, indexTv,time, title, label, favorite) = createRefs()
                Image(
                    painter = painterResource(R.mipmap.ic_account),
                    contentDescription = null,
                    modifier = Modifier
                        .size(20.dp)
                        .constrainAs(head) {
                            top.linkTo(parent.top)
                            start.linkTo(parent.start)
                        })
                Text(
                    text = "${index+1}",
                    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f),
                    fontSize = 12.sp,
                    modifier = Modifier.constrainAs(indexTv) {
                        top.linkTo(parent.top)
                        start.linkTo(head.end)
                    })
                Text(
                    text = "${TimeUtils.millis2String(data.publishTime)}",
                    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f),
                    fontSize = 12.sp,
                    modifier = Modifier.constrainAs(time) {
                        top.linkTo(parent.top)
                        end.linkTo(parent.end)
                    })

                Text(
                    text = "${data.title}",
//                    color = color_333,
                    fontSize = 14.sp,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.constrainAs(title) {
                        top.linkTo(head.bottom)
                        start.linkTo(parent.start)
                    })

                Text(
                    text = "广场Tab",
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 12.sp,
                    modifier = Modifier.constrainAs(label) {
                        top.linkTo(title.bottom)
                        start.linkTo(parent.start)
                    })

                Image(
                    imageVector = Icons.Default.Star,
                    contentDescription = null,
                    colorFilter = ColorFilter.tint(
                        if (data.collect) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.primaryContainer
                    ),
                    modifier = Modifier.constrainAs(favorite) {
                        top.linkTo(label.top)
                        end.linkTo(parent.end)
                    })

            }
        }
    }
}